package cn.wolfcode.p2p.website.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.ISystemDictionaryItemService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BasicInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/basicInfo")
    public String basicInfoPage(Model model){
        //userInfo
        model.addAttribute("userInfo", userInfoService.getCurrent());
        //educationBackgrounds
        //incomeGrades
        //marriages
        //kidCounts
        //houseConditions
        model.addAttribute("educationBackgrounds", systemDictionaryItemService.queryByParentSn("educationBackground"));
        model.addAttribute("incomeGrades", systemDictionaryItemService.queryByParentSn("incomeGrade"));
        model.addAttribute("marriages", systemDictionaryItemService.queryByParentSn("marriage"));
        model.addAttribute("kidCounts", systemDictionaryItemService.queryByParentSn("kidCount"));
        model.addAttribute("houseConditions", systemDictionaryItemService.queryByParentSn("houseCondition"));
        return "basicInfo";
    }


    @RequestMapping("/basicInfo_save")
    @ResponseBody
    public JSONResult saveBasicInfo(UserInfo userInfo){
        JSONResult result = null;
        try {
            userInfoService.saveBasicInfo(userInfo);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }

}
